<template>
	<span class="wtm-label-with-tooltip">
		{{ label }}
		<el-tooltip v-if="hasTooltip" :content="tooltip" :placement="placement" :show-after="showDelay" :hide-after="hideDelay">
			<el-icon
				class="wtm-label-tooltip-icon"
				:class="{ 'wtm-label-tooltip-icon--active': hasTooltip }"
				tabindex="0"
				role="button"
				:aria-label="`帮助信息: ${tooltip}`"
				@keydown.enter="$event.preventDefault()"
				@keydown.space="$event.preventDefault()"
			>
				<QuestionFilled />
			</el-icon>
		</el-tooltip>
	</span>
</template>

<script setup lang="ts">
import { computed } from 'vue';
import { QuestionFilled } from '@element-plus/icons-vue';

interface Props {
	label: string;
	tooltip?: string;
	placement?:
		| 'top'
		| 'top-start'
		| 'top-end'
		| 'bottom'
		| 'bottom-start'
		| 'bottom-end'
		| 'left'
		| 'left-start'
		| 'left-end'
		| 'right'
		| 'right-start'
		| 'right-end';
	showDelay?: number;
	hideDelay?: number;
}

const props = withDefaults(defineProps<Props>(), {
	placement: 'top',
	showDelay: 200,
	hideDelay: 100,
});

// 检查是否有有效的tooltip内容
const hasTooltip = computed(() => {
	return props.tooltip && props.tooltip.trim().length > 0;
});
</script>

<style scoped>
.wtm-label-with-tooltip {
	display: inline-flex;
	align-items: center;
	gap: 4px;
	/* 继承父级的所有文本样式 */
	color: inherit;
	font-size: inherit;
	font-weight: inherit;
	line-height: inherit;
	/* 防止换行 */
	white-space: nowrap;
	/* 确保在有限宽度内正常显示 */
	max-width: 100%;
	overflow: visible;
}

.wtm-label-tooltip-icon {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	width: 14px;
	height: 14px;
	font-size: 11px;
	color: var(--el-text-color-placeholder, #a8abb2);
	cursor: help;
	transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
	border-radius: 50%;
	outline: none;
	position: relative;
	background: transparent;
	flex-shrink: 0; /* 防止图标被压缩 */
	margin-left: 2px; /* 添加小间距 */
}

.wtm-label-tooltip-icon:hover {
	color: var(--el-color-primary, #409eff);
	background: var(--el-color-primary-light-9, #ecf5ff);
	transform: scale(1.1);
	box-shadow: 0 2px 4px rgba(64, 158, 255, 0.12);
}

.wtm-label-tooltip-icon:focus {
	color: var(--el-color-primary, #409eff);
	background: var(--el-color-primary-light-9, #ecf5ff);
	box-shadow: 0 0 0 2px var(--el-color-primary-light-8, #d9ecff);
	outline: none;
}

.wtm-label-tooltip-icon:active {
	transform: scale(1.05);
	transition-duration: 0.1s;
}

.wtm-label-tooltip-icon--active {
	opacity: 1;
}

/* 增强视觉反馈 */
.wtm-label-tooltip-icon::before {
	content: '';
	position: absolute;
	inset: -2px;
	border-radius: 50%;
	background: transparent;
	transition: background 0.2s ease;
	z-index: -1;
}

.wtm-label-tooltip-icon:hover::before {
	background: var(--el-color-primary-light-9, #ecf5ff);
}

/* 适配不同尺寸的表单 */
/* 大尺寸表单 */
:deep(.el-form--large) .wtm-label-tooltip-icon {
	width: 16px;
	height: 16px;
	font-size: 12px;
}

/* 小尺寸表单 */
:deep(.el-form--small) .wtm-label-with-tooltip {
	gap: 3px;
}

:deep(.el-form--small) .wtm-label-tooltip-icon {
	width: 12px;
	height: 12px;
	font-size: 10px;
}

/* 响应式设计 */
@media (max-width: 768px) {
	.wtm-label-with-tooltip {
		gap: 4px;
	}

	.wtm-label-tooltip-icon {
		width: 16px;
		height: 16px;
		font-size: 12px;
	}

	.wtm-label-tooltip-icon:hover {
		transform: scale(1.15);
	}
}

/* 高对比度模式支持 */
@media (prefers-contrast: high) {
	.wtm-label-tooltip-icon {
		color: var(--el-text-color-regular, #606266);
		border: 1px solid var(--el-border-color, #dcdfe6);
	}

	.wtm-label-tooltip-icon:hover,
	.wtm-label-tooltip-icon:focus {
		color: var(--el-color-primary, #409eff);
		border-color: var(--el-color-primary, #409eff);
		background: var(--el-color-white, #ffffff);
		box-shadow: 0 0 0 2px var(--el-color-primary, #409eff);
	}
}

/* 减少动画偏好支持 */
@media (prefers-reduced-motion: reduce) {
	.wtm-label-tooltip-icon {
		transition: color 0.2s ease;
	}

	.wtm-label-tooltip-icon:hover {
		transform: none;
	}

	.wtm-label-tooltip-icon::before {
		transition: none;
	}
}

/* 暗色主题适配 */
@media (prefers-color-scheme: dark) {
	.wtm-label-tooltip-icon:hover {
		background: rgba(64, 158, 255, 0.15);
		box-shadow: 0 2px 8px rgba(64, 158, 255, 0.2);
	}

	.wtm-label-tooltip-icon:focus {
		background: rgba(64, 158, 255, 0.15);
		box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.4);
	}
}

/* 禁用状态样式 */
.wtm-label-tooltip-icon[disabled] {
	color: var(--el-text-color-disabled, #c0c4cc);
	cursor: not-allowed;
	opacity: 0.6;
}

.wtm-label-tooltip-icon[disabled]:hover {
	color: var(--el-text-color-disabled, #c0c4cc);
	background: transparent;
	transform: none;
	box-shadow: none;
}

/* 聚焦指示器 */
.wtm-label-tooltip-icon:focus-visible {
	outline: 2px solid var(--el-color-primary, #409eff);
	outline-offset: 2px;
}
</style>
